/*
 * Creation : 19/09/2013
 */
/**
 * 
 */
package com.japcatalog.dao.impl;

import java.util.Collection;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.japcatalog.dao.ModelDao;
import com.japcatalog.model.Model;
import com.japcatalog.util.HibernateUtil;

/**
 * The Class ModelDaoImpl.
 * 
 * @author Fernando Ariel Perelli
 */
public class ModelDaoImpl implements ModelDao {

	/** The session. */
	private Session session;

	/**
	 * Getter session.
	 * 
	 * @return the session
	 */
	public Session getSession() {
		return session;
	}

	/**
	 * Sets the session.
	 * 
	 * @param session
	 *            the session to set
	 */
	public void setSession(Session session) {
		this.session = session;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.ModelDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Model> findAll() {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Collection<Model> models = null;
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			models = this.session.createCriteria(Model.class).list();
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		}
		return models;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.ModelDao#findById(java.lang.Long)
	 */
	@Override
	public Model findById(Long id) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Model model = null;
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			model = (Model) this.session.get(Model.class, id);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		}

		return model;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.ModelDao#create(com.japcatalog.model.Model)
	 */
	@Override
	public Long create(Model model) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();

		Transaction transaction = null;
		Long id = null;
		try {
			transaction = this.session.beginTransaction();
			id = (Long) this.session.save(model);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
		return id;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.ModelDao#update(com.japcatalog.model.Model)
	 */
	@Override
	public void update(Model model) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			this.session.update(model);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.ModelDao#delete(com.japcatalog.model.Model)
	 */
	@Override
	public void delete(Model model) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			this.session.delete(model);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}

	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.japcatalog.dao.ModelDao#delete(java.lang.Long)
	 */
	@Override
	public void delete(Long id) {
		this.session = HibernateUtil.getIntance().getSessionFactory().getCurrentSession();
		Model model = this.findById(id);
		Transaction transaction = null;
		try {
			transaction = this.session.beginTransaction();
			this.session.delete(model);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}

	}

}
